/**
 * Created by 15c on 2018/2/9.
 */
//用户学习记录页
app.controller('courserecord', function ($scope, $http) {
    var token = localStorage.getItem('token');
    //判断有无证书
    if (!token) {
        $.alert('请先登录', function () {
            location.href = '#login';
        });
        return;
    }
    //展示学习记录
    $scope.showStudy = function () {
        var infiniteScrollPreloader = $('.infinite-scroll-preloader');
        $http({
            method: 'get',
            url: app.netPath + '/userclass/show',
            headers: {Authorization: token},
        }).then(function (res) {
            // 删除加载提示符
            infiniteScrollPreloader.hide();
            console.log(res.data);
            if (!res.data.error) {
                if (res.data.data.length==0) {
                    $scope.listSize = 0
                }
                $scope.studyClassCount = res.data.data.length;
                $scope.studyClass = res.data.data;

                for (var i = 0; i < $scope.studyClass.length; i++) {
                    if ($scope.studyClass[i].aClass.classImg) {
                        $scope.studyClass[i].aClass.classImg = app.netPath + '/resources/classImg/' + $scope.studyClass[i].aClass.classImg;
                        app.buildImageUrl($scope.studyClass[i].aClass,'classImg',function () {
                            $scope.$digest();
                        });
                    }
                }

            }
            else {
                $.toast(res.data.msg)
            }
        }, function () {
            $.toast('网络走丢了！')
        })
    };

    //删除学习记录
    $scope.deleteClassRecord = function (classId) {
        $http({
            method: 'get',
            url: app.netPath + '/userclass/delete',
            headers: {Authorization: token},
            params: {classId: classId}
        }).then(function (res) {
            if (!res.data.error) {
                $.toast(res.data.msg);
                location.href = '#courserecord';
            }
            else {
                $.toast(res.data.msg)
            }
        })
    };
    //跳转课程详情页面
    $scope.getClassInfo = function (classId) {
        sessionStorage.setItem('classId', classId);
        location.href = '#xiangqing';
    };
});